package com.yst.gateway.monitor.common.security.extend;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.yst.gateway.monitor.common.utils.SpringMessageSource;

public class MyUserDetailServiceImpl implements UserDetailsService {
//	@Autowired
//	private UserService userService;
//	@Autowired
//	private RoleService roleService;
	private MessageSourceAccessor messages = SpringMessageSource.getAccessor(); 
	
	// 登入默认会调整到这里
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
//		HfSysUser hfSysUser= userService.getByName(username);
		Object hfSysUser=null;
		if(hfSysUser!=null){
//			if(hfSysUser.getStatus()==0){
//				throw new UsernameNotFoundException(messages.getMessage("hf.user.loginIn.invalid"));
//			}else if(hfSysUser.getStatus()==1){
//				throw new UsernameNotFoundException(messages.getMessage("hf.user.loginIn.lock"));
//			}
		}else{
			throw new UsernameNotFoundException(messages.getMessage("hf.user.loginIn.notExists").replace("{0}", username));
		}
		Collection<GrantedAuthority> grantedAuths = obtionGrantedAuthorities(hfSysUser);
		boolean enables = true;
		boolean accountNonExpired = true;
		boolean credentialsNonExpired = true;
		boolean accountNonLocked = true;
		LoginUser userdetail =null;
		try{
//			userdetail = new LoginUser(hfSysUser.getName(), hfSysUser.getPassword(),
//					enables, accountNonExpired, credentialsNonExpired,
//					accountNonLocked, grantedAuths,hfSysUser.getId());
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return userdetail;
	}

	// 取得用户的权限
	private Set<GrantedAuthority> obtionGrantedAuthorities(Object user) {
		Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
//		List<HfSysRole> roleList=roleService.getByUid(user.getId());
//		for(HfSysRole role:roleList){
//			authSet.add(new SimpleGrantedAuthority(role.getName()));
//		}
		return authSet;
	}
}